package com.exp.bookhouse.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exp.bookhouse.mapper.RoleMapper;
import com.exp.bookhouse.pojo.Permission;
import com.exp.bookhouse.pojo.Role;
import com.exp.bookhouse.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 根据用户名查询角色
     * @param userName
     * @return
     */
    @Override
    public List<Role> queryRolesByUserName(String userName) {
        return roleMapper.queryRolesByUserName(userName);
    }

    /**
     * 根据用户id查询角色
     * @param id
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<Role> queryRolesByUserIdAndPage(Long id, Integer page, Integer size) {
        IPage<Role> iPage = new Page<>(page,size);
        return roleMapper.queryRolesByUserIdAndPage(iPage,id);
    }

    /**
     * 分页查询角色信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<Role> queryRoleByPage(Integer page, Integer size) {
        IPage<Role> iPage = new Page<>(page,size);
        return roleMapper.selectPage(iPage,null);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @Override
    public int saveRole(Role role) {
        return roleMapper.insert(role);
    }

    /**
     * 根据id查询角色
     */
    @Override
    public Role queryRoleById(Long id) {
        return roleMapper.queryRoleById(id);
    }

    @Override
    public Role queryRoleByRoleName(String rolename){
        return roleMapper.queryRoleByRoleName(rolename);
    }

    /**
     * 查询角色可以添加的权限
     * @param roleId
     * @return
     */
    @Override
    public List<Permission> queryOtherPermissions(Long roleId) {
        return roleMapper.queryOtherPermissions(roleId);
    }

    /**
     * 角色添加权限
     * @param roleId
     * @param permissionIds
     */
    @Override
    public void saveRoleAddPermission(Long roleId, String[] permissionIds) {
        for (String permissionId : permissionIds) {
            long id = Long.parseLong(permissionId);
            roleMapper.saveRoleAddPermission(roleId,id);
        }
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @Override
    public int updateRole(Role role) {
        return roleMapper.updateById(role);
    }

    /**
     * 根据id删除角色
     * @param id
     */
    @Override
    public void deleteRoleById(Long id) {
        //1、查询角色拥有的权限
        List<Permission> permissions = roleMapper.queryPermissionsById(id);

        //2、删除权限-角色中间表
        roleMapper.deleteRolePermissionByRoleId(id);

        //3、删除权限
        for (Permission permission : permissions) {
            roleMapper.deleteById(permission.getId());
        }

        //4、删除用户-角色中间表
        roleMapper.deleteUserRoleByRoleId(id);

        //5、删除角色
        roleMapper.deleteById(id);
    }

}
